<?php

/**
 * This is the model class for table "{{cmp_templates}}".
 *
 * The followings are the available columns in table '{{cmp_templates}}':
 * @property integer $zct_id
 * @property string $zct_title
 * @property string $zct_name
 * @property string $zct_directory
 * @property integer $zct_type
 * @property string $zct_author
 * @property string $zct_desc
 * @property string $zmg_ids
 * @property integer $zct_listorder
 * @property integer $zct_default
 */
class CmpTemplates extends CActiveRecord
{
	//会员组
	public $zmg_id;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return CmpTemplates the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{cmp_templates}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zct_title,zmg_ids', 'required'),
			array('zct_type, zct_listorder, zct_default,inputtime,zct_status', 'numerical', 'integerOnly'=>true),
			array('zct_title', 'length', 'max'=>100),
			array('zct_name, zct_directory, zct_author', 'length', 'max'=>50),
			array('zct_desc', 'length', 'max'=>255),
			array('zmg_ids', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zct_id, zct_title, zct_name, zct_directory, zct_type, zct_author, zct_desc, zmg_ids, zct_listorder, zct_default', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(

		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zct_id' => 'Zct',
			'zct_title' => 'Zct Title',
			'zct_name' => 'Zct Name',
			'zct_directory' => 'Zct Directory',
			'zct_type' => 'Zct Type',
			'zct_author' => 'Zct Author',
			'zct_desc' => 'Zct Desc',
			'zmg_ids' => 'Zmg Ids',
			'zct_listorder' => 'Zct Listorder',
			'zct_status' => '状态',
			'zct_default' => 'Zct Default',
			'inputtime' => '创建时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zct_id',$this->zct_id);
		$criteria->compare('zct_title',$this->zct_title,true);
		$criteria->compare('zct_name',$this->zct_name,true);
		$criteria->compare('zct_directory',$this->zct_directory,true);
		$criteria->compare('zct_type',$this->zct_type);
		$criteria->compare('zct_author',$this->zct_author,true);
		$criteria->compare('zct_desc',$this->zct_desc,true);
		$criteria->compare('zmg_ids',$this->zmg_ids,true);
		$criteria->compare('zct_listorder',$this->zct_listorder);
		$criteria->compare('zct_status',$this->zct_status);
		$criteria->compare('zct_default',$this->zct_default);
		$criteria->compare('inputtime',$this->inputtime);

		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 获取商家的可用模板，暂无权限限制，待扩展
	 */
	public function getTemplatesByCmp($cmpid){
		//取当前商家对应的会员组ID， 还未绑定，默认测试为空
		$cmpZmgId = '';
		
		//全部可能模板
		$criteria=new CDbCriteria;
		$criteria->compare('zmg_ids',$this->zmg_ids,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 验证商家的模板使用权限，暂无权限限制，待扩展
	 * @param $tmpid 模板ID
	 * @param $userid 用户ID
	 */
	public function checkUsePower($tmpid,$userid){
		return true;
	}
	
	/**
	 * 是否启用
	 */
	public function getStatus($type=null){
		if($type==null){
			return array(
					'1'=>'是',
					'2'=>'否',
					);
		}else{
			$level = $this->getStatus();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 * 是否默认
	 */
	public function getDefault($type=null){
		if($type==null){
			return array(
					'1'=>'否',
					'2'=>'是',
			);
		}else{
			$level = $this->getDefault();
			if(array_key_exists($type,$level))
				return $level[$type];
		}
	}
	
	/**
	 * 查找数据后的操作根据会员等级，取会员组ID
	 */
	protected function afterFind(){
		$this->zmg_ids = explode(',',$this->zmg_ids);
		if(isset($this->zmg_ids[0])){
			$this->zmg_id = MemType::model()->findByPk($this->zmg_ids[0])->zmg_id;
		}
		return true;
	}
	
	/**
	 * 保存数据前的操作
	 */
	protected function beforeSave(){
		if($this->isNewRecord){
			$this->inputtime = time();
		}
		$this->zmg_ids = join(',',$this->zmg_ids);
		return true;
	}
}